<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class ProductFull extends Base
{

    protected $name = 'product_full';

    public static function savepsot($param)
    {
        parent::startTrans();
        try {
            $data['money'] = $param['money'];
            $data['title'] = $param['title'];
            $data['sort'] = $param['sort'];
            $data['sale_money'] = $param['sale_money'];
            $data['product_ids'] = implode(',', $param['product_ids']);
            if (isset($param['id']) && $param['id'] != 0) {
                $data['id'] = $param['id'];
                parent::update($data);
            } else {
                parent::create($data);
            }
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    //是否有优惠活动
    public static function getFull($id, $total)
    {
        parent::startTrans();
        try {
            $list = parent::where(['is_del' => 0])->select();
            $idslist = [];
            foreach ($list as $k => $v) {
                $ids = explode(',', $v['product_ids']);
                if (in_array($id, $ids)) {
                    if ($total >= $v['money']) {
                        $money[] = $v['money'];
                        $sale_money[] = $v['sale_money'];
                        $moneyyh[] = floor($total / $v['money']) * $v['sale_money'];
                        $title[] = $v['title'];
                    } else {
                        $money[] = 0;
                        $sale_money[] = 0;
                        $moneyyh[] = 0;
                        $title[] = '';
                    }
                    $titles[] = $v['title'];
                }
            }
            $max = max($sale_money);
            $key = array_search($max, $sale_money);
            $idslist['money'] = $money[$key];
            $idslist['sale_money'] = $sale_money[$key];
            $idslist['moneyyh'] = $moneyyh[$key];
            $idslist['title'] = $title[$key];
            $idslist['titlelist'] = $titles;
            return ['code' => 1, 'msg' => '操作成功！', 'list' => $idslist];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    //购物车满减

    public static function getFullCart($idlist, $ids)
    {
        parent::startTrans();
        try {
            $list = parent::where(['is_del' => 0])->select();
            $idslist = [];
            $total=0;
            foreach ($list as $k => $v) {
                $idsarr = explode(',', $v['product_ids']);
                $result = array_intersect($idsarr, $ids);
                $resultlist = array_values($result);
                foreach ($idlist as $kk => $vv) {
                    if (!empty($resultlist) && in_array($vv['pid'],$resultlist) && $vv['shoptype']=='shop') {
                        $total+=$vv['price'] * $vv['num'];
                        if ($total >= $v['money']) {
                            $money[] = $v['money'];
                            $sale_money[] = $v['sale_money'];
                            $yhid[] = $v['id'];
                            $title[] = $v['title'];
                        } else {
                            $money[] = 0;
                            $sale_money[] = 0;
                            $yhid[] = '';
                            $title[] = '';
                        }
                        $titles[] = $v['title'];
                    }
                }
            }
            $max = max($sale_money);
            $key = array_search($max, $sale_money);
            $idslist['money'] = $money[$key];
            $idslist['sale_money'] = $sale_money[$key];
            if($yhid[$key]>0){
                $finds=parent::where(['id'=>$yhid[$key],'is_del' => 0])->find();
                $moneyyh = floor($total / $finds['money']) * $finds['sale_money'];
            }else{
                $moneyyh=0;
            }
            $idslist['moneyyh'] = $moneyyh;
            $idslist['title'] = $title[$key];
            $idslist['titlelist'] = $titles;
            $idslist['total'] = $total;
            
            return ['code' => 1, 'msg' => '操作成功！', 'list' => $idslist];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
